package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.query.ProductQuery;
import com.ruoyi.system.service.IProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * 岗位信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@WebServlet("/fileUpload")
@RequestMapping("/system/product")
public class SysProductController extends BaseController
{
    private String prefix = "system/product";

    @Autowired
    private IProductService productService;

    @RequiresPermissions("system:post:view")
    @GetMapping()
    public String operlog() { return prefix + "/product"; }




    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductQuery qo){
        startPage();
        return getDataTable(productService.select());
    }



    @RequiresPermissions("system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long ids)
    {
        try
        {
            productService.deleteById(ids);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String product,MultipartFile bpmnFile,HttpServletRequest request) throws Exception {
        Part part = request.getPart("bpmnFile");
        productService.insert(product,part);

        return AjaxResult.success();
    }

    @PostMapping("/img")
    @ResponseBody
    public AjaxResult addSave(MultipartFile bpmnFile){
        System.out.println(bpmnFile);

        return AjaxResult.success();
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap mmap)
    {
        mmap.put("post", productService.get(postId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(String product,MultipartFile bpmnFile,HttpServletRequest request) throws Exception, ServletException {
        Part part = request.getPart("bpmnFile");
        productService.update(product,part);
    return AjaxResult.success();
    }
}
